﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using dbutility;
using DBUtility;
using System.Collections.Generic;
namespace hf.DAL
{
	/// <summary>
	/// 数据访问类:Article
	/// </summary>
	public partial class Article
	{
		public Article()
		{}
		#region  Method

        /// <summary>
        /// 
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="PageNumber"></param>
        /// <param name="strWhere"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<hf.Model.Article> GetArticleList(int PageSize, int PageNumber, string strWhere, out int total)
        {
            DataSet ds = PageHelper.GetList(PageSize, PageNumber, "tb_Article", "ID", strWhere, out total);

            //实例化列表
            List<hf.Model.Article> list = new List<hf.Model.Article>();

            //判断ds是不是空的
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    //把datatable转换成对象
                    hf.Model.Article m = DBHelperSQL.SetModel<hf.Model.Article>(dr);

                    if (m != null)
                        list.Add(m);
                }
            }

            return list;
        }

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DBHelperSQL.GetMaxID("ID", "tb_Article"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			int result= DBHelperSQL.RunProcedure("tb_Article_Exists",parameters,out rowsAffected);
			if(result==1)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		///  增加一条数据
		/// </summary>
		public int Add(hf.Model.Article model)
		{
			int rowsAffected;
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@LawyerID", SqlDbType.Int,4),
					new SqlParameter("@ArticleTypeID", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.VarChar,255),
					new SqlParameter("@Addtime", SqlDbType.DateTime),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@Hits", SqlDbType.Int,4),
					new SqlParameter("@isIndex", SqlDbType.Int,4),
					new SqlParameter("@Author", SqlDbType.VarChar,100) ,
                                       new SqlParameter("@isTop", SqlDbType.Int,4) ,
                                        new SqlParameter("@PicTitle", SqlDbType.VarChar,255),
                                        new SqlParameter("@CityID" ,SqlDbType.Int ,4)};
			parameters[0].Direction = ParameterDirection.Output;
			parameters[1].Value = model.LawyerID;
			parameters[2].Value = model.ArticleTypeID;
			parameters[3].Value = model.Title;
			parameters[4].Value = model.Addtime;
			parameters[5].Value = model.Content;
			parameters[6].Value = model.Hits;
			parameters[7].Value = model.isIndex;
			parameters[8].Value = model.Author;
            parameters[9].Value = model.isTop;
            parameters[10].Value = model.PicTitle;
            parameters[11].Value = model.CityID;

			DBHelperSQL.RunProcedure("tb_Article_ADD",parameters,out rowsAffected);
			return (int)parameters[0].Value;
		}

		/// <summary>
		///  更新一条数据
		/// </summary>
		public bool Update(hf.Model.Article model)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@LawyerID", SqlDbType.Int,4),
					new SqlParameter("@ArticleTypeID", SqlDbType.Int,4),
					new SqlParameter("@Title", SqlDbType.VarChar,255),
					new SqlParameter("@Addtime", SqlDbType.DateTime),
					new SqlParameter("@Content", SqlDbType.Text),
					new SqlParameter("@Hits", SqlDbType.Int,4),
					new SqlParameter("@isIndex", SqlDbType.Int,4),
					new SqlParameter("@Author", SqlDbType.VarChar,100) ,
                                       new SqlParameter("@isTop", SqlDbType.Int,4) ,
                                        new SqlParameter("@PicTitle", SqlDbType.VarChar,255),
                                        new SqlParameter("@CityID" ,SqlDbType.Int ,4)};
			parameters[0].Value = model.ID;
			parameters[1].Value = model.LawyerID;
			parameters[2].Value = model.ArticleTypeID;
			parameters[3].Value = model.Title;
			parameters[4].Value = model.Addtime;
			parameters[5].Value = model.Content;
			parameters[6].Value = model.Hits;
			parameters[7].Value = model.isIndex;
			parameters[8].Value = model.Author;
            parameters[9].Value = model.isTop;
            parameters[10].Value = model.PicTitle;
            parameters[11].Value = model.CityID;

			DBHelperSQL.RunProcedure("tb_Article_Update",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			int rowsAffected=0;
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			DBHelperSQL.RunProcedure("tb_Article_Delete",parameters,out rowsAffected);
			if (rowsAffected > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from tb_Article ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=DBHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hf.Model.Article GetModel(int ID)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4)
			};
			parameters[0].Value = ID;

			hf.Model.Article model=new hf.Model.Article();
			DataSet ds= DBHelperSQL.RunProcedure("tb_Article_GetModel",parameters,"ds");
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"]!=null && ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LawyerID"]!=null && ds.Tables[0].Rows[0]["LawyerID"].ToString()!="")
				{
					model.LawyerID=int.Parse(ds.Tables[0].Rows[0]["LawyerID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["ArticleTypeID"]!=null && ds.Tables[0].Rows[0]["ArticleTypeID"].ToString()!="")
				{
					model.ArticleTypeID=int.Parse(ds.Tables[0].Rows[0]["ArticleTypeID"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Title"]!=null && ds.Tables[0].Rows[0]["Title"].ToString()!="")
				{
					model.Title=ds.Tables[0].Rows[0]["Title"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Addtime"]!=null && ds.Tables[0].Rows[0]["Addtime"].ToString()!="")
				{
					model.Addtime=DateTime.Parse(ds.Tables[0].Rows[0]["Addtime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Content"]!=null && ds.Tables[0].Rows[0]["Content"].ToString()!="")
				{
					model.Content=ds.Tables[0].Rows[0]["Content"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Hits"]!=null && ds.Tables[0].Rows[0]["Hits"].ToString()!="")
				{
					model.Hits=int.Parse(ds.Tables[0].Rows[0]["Hits"].ToString());
				}
				if(ds.Tables[0].Rows[0]["isIndex"]!=null && ds.Tables[0].Rows[0]["isIndex"].ToString()!="")
				{
					model.isIndex=int.Parse(ds.Tables[0].Rows[0]["isIndex"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Author"]!=null && ds.Tables[0].Rows[0]["Author"].ToString()!="")
				{
					model.Author=ds.Tables[0].Rows[0]["Author"].ToString();
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,LawyerID,ArticleTypeID,Title,Addtime,Content,Hits,isIndex,Author ");
			strSql.Append(" FROM tb_Article ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DBHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" ID,LawyerID,ArticleTypeID,Title,Addtime,Content,Hits,isIndex,Author ");
			strSql.Append(" FROM tb_Article ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DBHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM tb_Article ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DBHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.ID desc");
			}
			strSql.Append(")AS Row, T.*  from tb_Article T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DBHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "tb_Article";
			parameters[1].Value = "ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DBHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

